package com.example.bluetooth.commonUtil;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by neil on 2018/2/8.
 */

public abstract class ClassicBluetoothUtil implements IBluetoothUtil{
    private BlueToothStateReceiver mReceiver;
    private String TAG = "ClassicBluetoothUtil";
    private Activity activity;
    private BluetoothManager mBluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;

    @Override
    public boolean init(Activity activity) {
        this.activity = activity;
        if (!this.activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
            Log.e(TAG, "不支持蓝牙");
            return false;
        }
        if (mBluetoothManager == null) {
            mBluetoothManager = (BluetoothManager) this.activity
                    .getSystemService(Context.BLUETOOTH_SERVICE);
        }
        if (mBluetoothManager == null) {
            return false;
        }
        if (mBluetoothAdapter == null) {
            mBluetoothAdapter = mBluetoothManager.getAdapter();
        }
        if (mBluetoothAdapter == null) {
            return false;
        }
        return true;
    }

    @Override
    public boolean isEnabled() {
        if (mBluetoothAdapter == null) {
            return false;
        }
        return mBluetoothAdapter.isEnabled();
    }

    @Override
    public boolean startScan() {
        if (mBluetoothAdapter == null) {
            return false;
        }
        showToast("正在寻找蓝牙设备");
        registerBluetoothReceiver();
        return mBluetoothAdapter.startDiscovery();
    }
    @Override
    public boolean stopScan() {
        if (mBluetoothAdapter == null) {
            return false;
        }
        unregisterBluetoothReceiver();
        return mBluetoothAdapter.cancelDiscovery();
    }

    @Override
    public abstract void onFoundDevices(BluetoothDevice devices, int rssi, byte[] scanRecord);

    public void registerBluetoothReceiver() {
        if (mReceiver == null)
            mReceiver = new BlueToothStateReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);//搜多到蓝牙
        activity.registerReceiver(mReceiver, filter);
    }

    public void unregisterBluetoothReceiver() {
        try {
            activity.unregisterReceiver(mReceiver);
            mReceiver=null;
        } catch (Exception e) {
        }
    }

    class BlueToothStateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG, device.getAddress());
                onFoundDevices(device,-100,null);
            }
        }
    }

    private void showToast(final String msg) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
